﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using App1.DataModel;

// The data model defined by this file serves as a representative example of a strongly-typed
// model that supports notification when members are added, removed, or modified.  The property
// names chosen coincide with data bindings in the standard item templates.
//
// Applications may use this model as a starting point and build on it, or discard it entirely and
// replace it with something appropriate to their needs.

namespace App1.Data
{

    [Windows.Foundation.Metadata.WebHostHidden]    


    /// <summary>
    /// Creates a collection of groups and items with hard-coded content.
    /// </summary>
    public sealed class DataControllerTemp
    {
        private static DataControllerTemp _dataController = new DataControllerTemp();

        public DataControllerTemp()    {

        }

        private bool initialized = false;
        private ObservableCollection<GroupTag> _allChannels = new ObservableCollection<GroupTag>();
        public ObservableCollection<GroupTag> AllChannels
        {
            get { return this._allChannels; }
        }
 
        private ObservableCollection<GroupEvent> _Events = new ObservableCollection<GroupEvent>();
        public ObservableCollection<GroupEvent> Events
        {
            get { return this._Events; }
        }

        public void initialize()  {
            String ITEM_CONTENT = String.Format("Item Content: {0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}",
                       "Curabitur class aliquam vestibulum nam curae maecenas sed integer cras phasellus suspendisse quisque donec dis praesent accumsan bibendum pellentesque condimentum adipiscing etiam consequat vivamus dictumst aliquam duis convallis scelerisque est parturient ullamcorper aliquet fusce suspendisse nunc hac eleifend amet blandit facilisi condimentum commodo scelerisque faucibus aenean ullamcorper ante mauris dignissim consectetuer nullam lorem vestibulum habitant conubia elementum pellentesque morbi facilisis arcu sollicitudin diam cubilia aptent vestibulum auctor eget dapibus pellentesque inceptos leo egestas interdum nulla consectetuer suspendisse adipiscing pellentesque proin lobortis sollicitudin augue elit mus congue fermentum parturient fringilla euismod feugiat");

            var group1 = new GroupTag("Group-1",
                    "Canals",
                    "Diferents canals d'activitats...",
                    "Assets/MediumGray.png",
                    "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
            group1.Tags.Add(new Tag("Cinema",
                    "Cinema",
                    "Sessions de cine, curtmetratges...",
                    "Assets/Cinema.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat."
                    ));
            group1.Tags.Add(new Tag("Musica",
                    "Música",
                    "Concerts, festivals, balls...",
                    "Assets/Anscari.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat."
                    ));
            group1.Tags.Add(new Tag("Gastronomia",
                    "Gastronomia",
                    "Tasts, festes gastronòmiques...",
                    "Assets/Gastronomia.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat."
                    ));
            group1.Tags.Add(new Tag("Exposicions",
                    "Exposicions",
                    "museus, exposicions, performances",
                    "Assets/Exposicions.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat."
                    ));
                    _dataController.AllChannels.Add(group1);

                    // TODO: FER LA CRIDA PER RETORNAR GETEVENTS
                    //String ITEM_CONTENT = String.Format("Item Content: {0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}",
                    //            "Curabitur class aliquam vestibulum nam curae maecenas sed integer cras phasellus suspendisse quisque donec dis praesent accumsan bibendum pellentesque condimentum adipiscing etiam consequat vivamus dictumst aliquam duis convallis scelerisque est parturient ullamcorper aliquet fusce suspendisse nunc hac eleifend amet blandit facilisi condimentum commodo scelerisque faucibus aenean ullamcorper ante mauris dignissim consectetuer nullam lorem vestibulum habitant conubia elementum pellentesque morbi facilisis arcu sollicitudin diam cubilia aptent vestibulum auctor eget dapibus pellentesque inceptos leo egestas interdum nulla consectetuer suspendisse adipiscing pellentesque proin lobortis sollicitudin augue elit mus congue fermentum parturient fringilla euismod feugiat");

                    List<Tag> tags = new List<Tag>();
                    tags.Add(new Tag("Musica",
                            "Música",
                            "Concerts, festivals, balls...",
                            "Assets/LightGray.png",
                            "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat."
                            ));

                    var group2 = new GroupEvent("Group-2",
                    "Esdeveniments de música",
                    "Concerts, festivals, balls...",
                    "Assets/DarkGray.png",
                    "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
                    Event ev1 = new Event("Group-2-Item-0",
                             "The Blue Pie",
                             "Psicodèlic, Blues, Rock.",
                             "Assets/Blue Pie.png",
                             "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                             tags, group2);
                    ev1.Latitude = 41.402;
                    ev1.Longitude = 2.203;
                    group2.Events.Add(ev1);
                    Event ev2 = new Event("Group-2-Item-1",
                            "APAC",
                            "Progressiu, Funk",
                            "Assets/Anscari.png",
                            "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                            tags, group2);
                    group2.Events.Add(ev2);
                    ev2.Latitude = 41.408;
                    ev2.Longitude = 2.209;
                    Event ev3 = new Event("Group-2-Item-2",
                            "Pink Floyd",
                            "Psicodèlic, Progressiu",
                            "Assets/Pink+Floyd.png",
                            "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                            tags, group2);
                    group2.Events.Add(ev3);
                    ev3.Latitude = 41.40;
                    ev3.Longitude = 2.20;
                    _dataController.Events.Add(group2);


        }

        /* -------------------------------------------------------------------------------------- */
        /*                              Retorna tots els tags generals                              */
        /* -------------------------------------------------------------------------------------- */
        
        public static IEnumerable<GroupTag> GetChannels(string uniqueId)
        {
            if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");

            // TODO: ESBORRAR TOT AIXÒ TAN BON PUNT ESTIGUI LA BD
            if (!_dataController.initialized)
            {               
                _dataController.initialize();
                _dataController.initialized = true;
            }
            return _dataController.AllChannels;
        }


        public static IEnumerable<GroupTag> GetButtons()
        {
            String ITEM_CONTENT = String.Format("Item Content: {0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}",
                        "Curabitur class aliquam vestibulum nam curae maecenas sed integer cras phasellus suspendisse quisque donec dis praesent accumsan bibendum pellentesque condimentum adipiscing etiam consequat vivamus dictumst aliquam duis convallis scelerisque est parturient ullamcorper aliquet fusce suspendisse nunc hac eleifend amet blandit facilisi condimentum commodo scelerisque faucibus aenean ullamcorper ante mauris dignissim consectetuer nullam lorem vestibulum habitant conubia elementum pellentesque morbi facilisis arcu sollicitudin diam cubilia aptent vestibulum auctor eget dapibus pellentesque inceptos leo egestas interdum nulla consectetuer suspendisse adipiscing pellentesque proin lobortis sollicitudin augue elit mus congue fermentum parturient fringilla euismod feugiat");

            var group1 = new GroupTag("Group-1",
                    "Home",
                    "",
                    "Assets/DarkGray.png",
                    "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
            group1.Tags.Add(new Tag("NextEvent",
                    "Next Event",
                    "Next events.",
                    "Assets/LightGray.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat."
                    ));
            group1.Tags.Add(new Tag("AllEvents",
                    "All Events",
                    "Concerts, festivals, balls...",
                    "Assets/LightGray.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat."
                    ));
            yield return group1;
        }

        public static IEnumerable<Event> GetUserEvents(List<string> tags)
        {
            if (tags.Any())
            {
                foreach (var item in tags)
                {
                    foreach (var aux in DataControllerTemp.GetEvents(item))
                    {
                        yield return aux;
                    }
                }
            }
            else
            {
                yield return null;
            }
        }


        /* -------------------------------------------------------------------------------------- */
        /*                  Retorna els events que tinguin el tag uniqueId                          */
        /* -------------------------------------------------------------------------------------- */
        public static IEnumerable<Event> GetEvents(string uniqueId)
        {

            if (!_dataController.initialized)
            {
                _dataController.initialize();
                _dataController.initialized = true;
            }
            return _dataController._Events.SelectMany(group => group.Events).Where((item) => item.Channels.Any(x => x.UniqueId.Equals(uniqueId)));

        }


        /* -------------------------------------------------------------------------------------- */
        /*                  Retorna un Tag donada la seva id                                        */
        /* -------------------------------------------------------------------------------------- */
       public static Tag GetChannel(string uniqueId)
        {
            // Simple linear search is acceptable for small data sets
            var matches = _dataController.AllChannels.SelectMany(group => group.Tags).Where((channel) => channel.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }

        public static Event GetItem(string uniqueId)
        {
            // Simple linear search is acceptable for small data sets
            var matches = _dataController._Events.SelectMany(group => group.Events).Where((item) => item.UniqueId.Equals(uniqueId));
            return matches.First();
        }

    }
}
